<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/10">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/31. shell 练习-批量杀进程/">shell 练习-批量杀进程</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>今天发现网站访问超级慢，top看如下：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E7%BB%83%E4%B9%A0-%E6%89%B9%E9%87%8F%E6%9D%80%E8%BF%9B%E7%A8%8B/01.jpg?raw=true" alt=""></figure></p>
<p>有很多sh进程，再ps查看：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E7%BB%83%E4%B9%A0-%E6%89%B9%E9%87%8F%E6%9D%80%E8%BF%9B%E7%A8%8B/02.jpg?raw=true" alt=""></figure></p>
<p>这个脚本，运行很慢，因为制定了cron，上一次还没有运行完，又有了新的运行任务。太多肯定会导致系统负载升高。当务之急就是先把这些在跑的给kill掉。那么请写一个脚本，直接杀死所有的sh。 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">ps aux |grep clearmem.sh |grep -v grep|awk <span class="string">'&#123;print $2&#125;'</span>|xargs <span class="built_in">kill</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/30. shell 练习-统计网卡流量/">shell 练习-统计网卡流量</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个脚本,检测你的网络流量，并记录到一个日志里。需要按照如下格式，并且一分钟统计一次（只需要统计外网网卡，假设网卡名字为eth0)：</p>
<p>2017-08-04 01:11<br>eth0 input: 1000bps<br>eth0 output : 200000bps    </p>
<p>################<br>2017-08-04 01:12<br>eth0 input: 1000bps<br>eth0 output : 200000bps    </p>
<p>提示：使用sar -n DEV  1 59 这样可以统计一分钟的平均网卡流量，只需要最后面的平均值。另外，注意换算一下，1byt=8bit</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">while</span> :</div><div class="line"><span class="keyword">do</span></div><div class="line">    DATE=`date +<span class="string">"%Y-%m-%d %H:%M"</span>`</div><div class="line">    LOG_PATH=/tmp/traffic_check/`date +%Y%m`</div><div class="line">    LOG_FILE=<span class="variable">$LOG_PATH</span>/traffic_check_`date +%d`.<span class="built_in">log</span></div><div class="line"></div><div class="line">    [ -d <span class="variable">$LOG_PATH</span>] || mkdir -p <span class="variable">$LOG_PATH</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">" <span class="variable">$DATE</span>"</span> &gt;&gt; <span class="variable">$LOG_FILE</span></div><div class="line">    sar -n DEV 1 59|grep Average|grep eth0|awk <span class="string">'&#123;print "\n",$2,"\t","input:",$5*1000*8,"bps","\t","\n",$2,"\t","output:",$6*1000*8,"bps" &#125;'</span> &gt;&gt; <span class="variable">$LOG_FILE</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"#####################"</span> &gt;&gt; <span class="variable">$LOG_FILE</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/3. shell 练习-计算进程占用内存和/">shell 练习-计算进程占用内存和</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>ps 可以查看进程的内存占用大小，写一个脚本计算一下所有进程所占用内存大小的和。（提示，使用ps aux 列出所有进程，过滤出RSS那列，然后求和）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#! /bin/bash</span></div><div class="line">sum=0</div><div class="line"><span class="keyword">for</span> mem <span class="keyword">in</span> `ps aux |awk <span class="string">'&#123;print $6&#125;'</span> |grep -v <span class="string">'RSS'</span>`</div><div class="line"><span class="keyword">do</span></div><div class="line">    sum=$[<span class="variable">$sum</span>+<span class="variable">$mem</span>]</div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"The total memory is <span class="variable">$sum</span>"</span><span class="string">"k"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以一条 awk 命令完成</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ps aux|awk <span class="string">'&#123;print $6&#125;'</span>|awk <span class="string">'&#123;(sum=sum+$1)&#125;;END&#123;print sum&#125;'</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/29. shell 练习-检测文件改动/">shell 练习-检测文件改动</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>有两台Linux服务器A和B，假如A可以直接ssh到B，不用输入密码。A和B都有一个目录叫做/data/web/ 这下面有很多文件，当然我们不知道具体有几层子目录，假若之前A和B上该目录下的文件都是一模一样的。但现在不确定是否一致了。固需要我们写一个脚本实现这样的功能，检测A机器和B机器/data/web/目录下文件的异同，我们以A机器上的文件作为标准。比如，假若B机器少了一个a.txt文件，那我们应该能够检测出来，或者B机器上的b.txt文件有过改动，我们也应该能够检测出来（B机器上多了文件我们不用考虑）。</p>
<p>提示： 使用核心命令  md5sum a.txt  算出md5值，去和B机器上的比较。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#假设A机器到B机器已经做了无密码登录设置</span></div><div class="line">dir=/data/web</div><div class="line"><span class="comment">##假设B机器的IP为192.168.0.100</span></div><div class="line">B_ip=192.168.0.100</div><div class="line">find <span class="variable">$dir</span> -<span class="built_in">type</span> f |xargs md5sum &gt;/tmp/md5.txt</div><div class="line">ssh <span class="variable">$B_ip</span> <span class="string">"find <span class="variable">$dir</span> -type f |xargs md5sum &gt;/tmp/md5_b.txt"</span></div><div class="line">scp <span class="variable">$B_ip</span>:/tmp/md5_b.txt /tmp</div><div class="line"></div><div class="line"><span class="keyword">for</span> f <span class="keyword">in</span> `awk <span class="string">'&#123;print $2&#125;'</span> /tmp/md5.txt`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="keyword">if</span> grep -q <span class="string">"<span class="variable">$f</span>"</span> /tmp/md5_b.txt</div><div class="line">    <span class="keyword">then</span></div><div class="line">        md5_a=`grep <span class="variable">$f</span> /tmp/md5.txt|awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line">        md5_b=`grep <span class="variable">$f</span> /tmp/md5_b.txt|awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line">        <span class="keyword">if</span> [ <span class="variable">$md5_a</span> != <span class="variable">$md5_b</span> ]</div><div class="line">        <span class="keyword">then</span></div><div class="line">             <span class="built_in">echo</span> <span class="string">"<span class="variable">$f</span> changed."</span></div><div class="line">        <span class="keyword">fi</span></div><div class="line">    <span class="keyword">else</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"<span class="variable">$f</span> deleted. "</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/28. shell 练习-统计数字并求和/">shell 练习-统计数字并求和</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>计算文档a.txt中每一行中出现的数字个数并且要计算一下整个文档中一共出现了几个数字。例如a.txt内容如下：</p>
<p>12aa*lkjskdj<br>alskdflkskdjflkjj</p>
<p>我们脚本名字为 ncount.sh, 运行它时：</p>
<p>bash ncount.sh a.txt</p>
<p>输入结果应该为：</p>
<p>2<br>0<br>sum:2</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">n=`wc -l a.txt|awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line">sum=0</div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 <span class="variable">$n</span>`</div><div class="line"><span class="keyword">do</span></div><div class="line">    line=`sed -n <span class="string">"<span class="variable">$i</span>"</span>p a.txt`</div><div class="line">    n_n=`<span class="built_in">echo</span> -n <span class="variable">$line</span>|sed <span class="string">'s/[^0-9]//g'</span>|wc -c`</div><div class="line">    <span class="built_in">echo</span> line <span class="variable">$i</span> number: <span class="variable">$n_n</span></div><div class="line">    sum=$[<span class="variable">$sum</span>+<span class="variable">$n_n</span>]</div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> sum is <span class="variable">$sum</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/27. shell 练习-统计日志大小/">shell 练习-统计日志大小</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>假如我们需要每小时都去执行你写的脚本。在脚本中实现这样的功能，当时间是0点和12点时，需要将目录/data/log/下的文件全部清空，注意只能清空文件内容而不能删除文件。而其他时间只需要统计一下每个文件的大小，一个文件一行，输出到一个按日期和时间为名字的日志里。 需要考虑/data/log/目录下的二级、三级、…  等子目录里面的文件。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">logdir=<span class="string">"/data/log"</span></div><div class="line">t=`date +%H`</div><div class="line">d=`date +%F-%H`</div><div class="line">[ -d /tmp/log_size || mkdir /tmp/log_size ]</div><div class="line"></div><div class="line"><span class="keyword">for</span> <span class="built_in">log</span> <span class="keyword">in</span> `find <span class="variable">$logdir</span> -<span class="built_in">type</span> f`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$t</span> == <span class="string">"0"</span> ] || [ <span class="variable">$t</span> == <span class="string">"10"</span> ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">    <span class="literal">true</span> &gt; <span class="variable">$log</span></div><div class="line">    <span class="keyword">else</span></div><div class="line">    du -sh <span class="variable">$log</span> &gt;&gt;/tmp/log_size/<span class="variable">$d</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/26. shell 练习-统计最常用命令/">shell 练习-统计最常用命令</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个shell脚本来看看你使用最多的命令是哪些，列出你最常用的命令top10。</p>
<p>思路：我们要用到一个文件就是.bash_history，然后再去sort、uniq，剩下的就不用多说了吧。很简单一个shell。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sort /root/.bash_history |uniq -c |sort -nr |head</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/25. shell 练习-获取文件列表/">shell 练习-获取文件列表</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>有一台服务器作为web应用，有一个目录（/data/web/attachment）不定时地会被用户上传新的文件，但是不知道什么时候会上传。所以，需要我们每5分钟做一次检测是否有新文件生成。</p>
<p>请写一个shell脚本去完成检测。检测完成后若是有新文件，还需要将新文件的列表输出到一个按年、月、日、时、分为名字的日志里。请不要想的太复杂，核心命令只有一个 find /data/web/attachment -mmin -5 </p>
<p>思路： 每5分钟检测一次，那肯定需要有一个计划任务，每5分钟去执行一次。脚本检测的时候，就是使用find命令查找5分钟内有过更新的文件，若是有更新，那这个命令会输出东西，否则是没有输出的。固，我们可以把输出结果的行数作为比较对象，看看它是否大于0。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">d=`date -d <span class="string">"-5 min"</span> +%Y%m%d%H%M`</div><div class="line">basedir=/data/web/attachment</div><div class="line">find <span class="variable">$basedir</span>/ -<span class="built_in">type</span> f  -mmin -5 &gt; /tmp/newf.txt</div><div class="line">n=`wc -l /tmp/newf.txt`</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$n</span> -gt 0 ]; <span class="keyword">then</span></div><div class="line">    /bin/mv /tmp/newf.txt /tmp/<span class="variable">$d</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/24. shell 练习-监控磁盘使用率/">shell 练习-监控磁盘使用率</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个shell脚本，检测所有磁盘分区使用率和inode使用率并记录到以当天日期为命名的日志文件里，当发现某个分区容量或者inode使用量大于85%时，发邮件通知你自己。</p>
<p>思路：就是先df -h 然后过滤出已使用的那一列，然后再想办法过滤出百分比的整数部分，然后和85去比较，同理，inode也是一样的思路。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">## This script is for record Filesystem Use%,IUse% everyday and send alert mail when % is more than 85%.</span></div><div class="line"></div><div class="line"><span class="built_in">log</span>=/var/<span class="built_in">log</span>/disk/`date +%F`.<span class="built_in">log</span></div><div class="line">date +<span class="string">'%F %T'</span> &gt; <span class="variable">$log</span></div><div class="line">df -h &gt;&gt; <span class="variable">$log</span></div><div class="line"><span class="built_in">echo</span> &gt;&gt; <span class="variable">$log</span></div><div class="line">df -i &gt;&gt; <span class="variable">$log</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `df -h|grep -v <span class="string">'Use%'</span>|sed <span class="string">'s/%//'</span>|awk <span class="string">'&#123;print $5&#125;'</span>`; <span class="keyword">do</span></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$i</span> -gt 85 ]; <span class="keyword">then</span></div><div class="line">        use=`df -h|grep -v <span class="string">'Use%'</span>|sed <span class="string">'s/%//'</span>|awk <span class="string">'$5=='</span><span class="variable">$i</span><span class="string">' &#123;print $1,$5&#125;'</span>`</div><div class="line">        <span class="built_in">echo</span> <span class="string">"<span class="variable">$use</span>"</span> &gt;&gt; use</div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="keyword">if</span> [ -e use ]; <span class="keyword">then</span></div><div class="line">   <span class="comment">##这里可以使用咱们之前介绍的mail.py发邮件</span></div><div class="line">    mail -s <span class="string">"Filesystem Use% check"</span> root@localhost &lt; use</div><div class="line">    rm -rf use</div><div class="line"><span class="keyword">fi</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> j <span class="keyword">in</span> `df -i|grep -v <span class="string">'IUse%'</span>|sed <span class="string">'s/%//'</span>|awk <span class="string">'&#123;print $5&#125;'</span>`; <span class="keyword">do</span></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$j</span> -gt 85 ]; <span class="keyword">then</span></div><div class="line">        iuse=`df -i|grep -v <span class="string">'IUse%'</span>|sed <span class="string">'s/%//'</span>|awk <span class="string">'$5=='</span><span class="variable">$j</span><span class="string">' &#123;print $1,$5&#125;'</span>`</div><div class="line">        <span class="built_in">echo</span> <span class="string">"<span class="variable">$iuse</span>"</span> &gt;&gt; iuse</div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="keyword">if</span> [ -e iuse ]; <span class="keyword">then</span></div><div class="line">    mail -s <span class="string">"Filesystem IUse% check"</span> root@localhost &lt; iuse</div><div class="line">    rm -rf iuse</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>思路：</p>
<ol>
<li>df -h、df -i 记录磁盘分区使用率和inode使用率，date +%F 日志名格式</li>
<li>取出使用率(第5列)百分比序列，for循环逐一与85比较，大于85则记录到新文件里，当for循环结束后，汇总超过85的一并发送邮件(邮箱服务因未搭建，发送本地root账户)。</li>
</ol>
<p>此脚本正确运行前提：该系统没有逻辑卷的情况下使用，因为逻辑卷df -h、df -i 时，使用率百分比是在第4列，而不是第5列。如有逻辑卷，则会漏统计逻辑卷使用情况。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/23. shell 练习-统计普通用户/">shell 练习-统计普通用户</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写个shell，看看你的Linux系统中是否有自定义用户（普通用户），若是有，一共有几个？</p>
<p>假设所有普通用户都是uid大于1000的</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">n=`awk -F <span class="string">':'</span> <span class="string">'$3&gt;1000'</span> /etc/passwd|wc -l`</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$n</span> -gt 0 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"There are <span class="variable">$n</span> common users."</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"No common users."</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	

</article>





	<span class="different-posts">📖 <a href="/page/12">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
